import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  UploadedFile,
  HttpException, HttpStatus
} from "@nestjs/common";
import { UploadService } from './upload.service';
import { FileInterceptor } from '@nestjs/platform-express';
import * as path from 'path'
import * as process from "node:process";
import { diskStorage } from "multer";
import {v4 as uuidv4} from 'uuid'
import { ConfigService } from "@nestjs/config";

@Controller('upload')
export class UploadController {
  constructor(
    private readonly uploadService: UploadService,
    private readonly configService: ConfigService
  ) {}

  @Post("image")
  @UseInterceptors(
    FileInterceptor('file',{
      storage: diskStorage({
        destination: path.resolve(process.cwd(),'public/images'),
        filename: (req, file, callback) => {
          const fileName: string = `${new Date().getTime()}-${uuidv4().slice(0, 8)}${path.extname(file.originalname)}`;
          return callback(null, fileName);
        },
      }),
    })
  )
  createImage(@UploadedFile() file: Express.Multer.File) {
    console.log(file);
    return Object.assign({},file,{url:`${this.configService.get<string>('FILE_URL')}/public/images/${file?.filename}`})
  }
}
